package com.example.neusoft.cook.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.cook.Adapter.FoodListAdapter;
import com.example.neusoft.cook.R;
import com.example.neusoft.cook.base.BaseAcyivity;
import com.example.neusoft.cook.bean.FavorShop;
import com.example.neusoft.cook.bean.Favoruse;
import com.example.neusoft.cook.bean.SampleCook;
import com.example.neusoft.cook.interf.Config;
import com.example.neusoft.cook.interf.RetrofitService;

import java.util.List;
import java.util.StringTokenizer;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodListActivity extends BaseAcyivity {
    private TextView titleView;
    private ImageButton favorButton;
    private RecyclerView recyclerView;
    private FoodListAdapter adapter;
    private int shopId;
    private String shopName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list);

        shopId = getIntent().getIntExtra("shop_id",-1);//获取shop_id
        shopName = getIntent().getStringExtra("shop_name");

        titleView = (TextView) findViewById(R.id.textView_shop_name);
        titleView.setText(shopName);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        favorButton = (ImageButton) findViewById(R.id.imageButtom_favor);
        favorButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击收藏按钮，改变收藏状态
                doFavor(userId, String.valueOf(shopId));
        }
        });



        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);

        Call<List<SampleCook>> call = retrofitService.getFoodByShop(String.valueOf(shopId));
        call.enqueue(new Callback<List<SampleCook>>() {
            @Override
            public void onResponse(Call<List<SampleCook>> call, Response<List<SampleCook>> response) {
                adapter = new FoodListAdapter(FoodListActivity.this,response.body());
                LinearLayoutManager layoutManager = new LinearLayoutManager(FoodListActivity.this);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(adapter);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                //获取完店铺详情后，再请求获取收藏状态
                getCollectionState(userId, String.valueOf(shopId));

            }

            @Override
            public void onFailure(Call<List<SampleCook>> call, Throwable t) {

            }
        });
    }

    private void getCollectionState(String userId, String shoopFoodId ){
        //定义Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //获取Retrofit接口
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<Favoruse> call = retrofitService.isCollected(userId, shoopFoodId, "0");
        call.enqueue(new Callback<Favoruse>() {
            @Override
            public void onResponse(Call<Favoruse> call, Response<Favoruse> response) {
                //获取店铺收藏状态
                if (response.body().getCollected().equals("1")){
                    //已收藏
                    favorButton.setImageResource(R.drawable.ic_collect);
                }
                else{
                    //未收藏
                    favorButton.setImageResource(R.drawable.ic_collect_no);
                }

            }

            @Override
            public void onFailure(Call<Favoruse> call, Throwable t) {

            }
        });

    }
    private void doFavor(String userId, String shopId){
        //定义Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Config.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //获取Retrofit接口
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<FavorShop> call =retrofitService.userCollectShop(userId,shopId);
        call.enqueue(new Callback<FavorShop>() {
            @Override
            public void onResponse(Call<FavorShop> call, Response<FavorShop> response) {
                if (response.body().getSuccess().equals("1")){
                    //操作成功
                    Toast.makeText(FoodListActivity.this,"操作成功",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<FavorShop> call, Throwable t) {

            }
        });
    }

}
